package com.geyue.controller;

import com.geyue.common.cache.Cache;
import com.geyue.common.toolbox.result.Res;
import com.geyue.common.util.SmsUtil;
import com.geyue.common.util.StrUtils;
import com.geyue.core.model.Teacher;
import com.geyue.core.service.TeacherService;
import org.nutz.lang.random.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/5/25.
 */
@Controller
public class IndexController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 跳转登录
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "login";
    }

    /**
     * 跳转登录
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    /**
     * 登录
     * @param mobile
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String mobile, String password, HttpSession session) {

        Teacher teacher = new Teacher();
        teacher.setMobile(mobile);
        teacher.setPassword(password);

        Teacher login = teacherService.getOne(teacher);

        if(login != null){
            session.setAttribute("teacher",login);
            return "redirect:main";
        }

        return "/login";
    }

    @RequestMapping("/main.html")
    public String main1(HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "error";

        return "main";
    }

    @RequestMapping(value = "/out", method = RequestMethod.GET)
    public String out(HttpSession session) {
        session.invalidate();
        return "redirect:login";
    }

    /**
     * 跳转注册
     * @return
     */
    @RequestMapping(value = "/reg", method = RequestMethod.GET)
    public String reg() {
        return "reg";
    }

    /**
     * 教师注册
     * @return
     */
    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public Res reg(String mobile,String password,String msgCode) {
        //取出内存中code
        String code = Cache.getSingleCode().get(mobile);

        if(StrUtils.isEmpty(code) || !code.equals(msgCode))
            return Res.fail("验证码错误,请重新尝试!");

        Teacher teacher = new Teacher();
        teacher.setMobile(mobile);
        teacher.setPassword(password);
        teacher.setMsgCode(msgCode);

        if(teacherService.saveNotEmpty(teacher) > 0)
            return Res.success("注册成功!");

        return Res.fail("注册失败!");
    }

    @RequestMapping(value = "/msg", method = RequestMethod.POST)
    @ResponseBody
    public Object msg(String mobile) {

        if (StrUtils.isEmpty(mobile) || mobile.length() != 11)
            return Res.fail("手机号码格式错误!");

        //验证上次发送时间
        boolean isOk = Cache.msg_send_validate(mobile);

        if(!isOk)
            return Res.fail("请稍候重试!");
        //发送
        String code = SmsUtil.send(mobile);

        if("-1".equals(code))
            return Res.fail("发送失败,请稍候重试!");
        //放入内存
        Cache.getSingleCode().put(mobile,code);

        return Res.success("发送成功!",code);
    }

    /**
     * 跳转登录
     * @return
     */
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String main(HttpSession session, Model model) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "redirect:login";

        model.addAttribute("teacher",teacherService.getOneByKey(teacher.getId()));
        return "index";
    }
}
